﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace canyinguanlixitong
{
    public partial class kcwm快餐外卖 : Form
    {
        public kcwm快餐外卖()
        {
            InitializeComponent();
        }
        string str = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
        private void kcwm快餐外卖_Load(object sender, EventArgs e)
        {
            chakan();
            this.panel1.Visible = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            POS结账 frm = new POS结账();
            frm.ggz = Convert.ToString(this.dataGridView1.SelectedRows[0].Cells["jiage"].Value);
            frm.spID = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["spID"].Value);
            MessageBox.Show(frm.ggz);
            frm.Show();
        }
        DataSet hj = new DataSet("cj");
        public int pig;
        public void chakan()
        {
            try
            {
                SqlConnection con = new SqlConnection(str);
                string akn = @"select spID,spName,spJia,spLei from commodity";
                SqlDataAdapter an = new SqlDataAdapter(akn, con);
                if (hj.Tables["cj"] != null)
                {
                    hj.Tables["cj"].Clear();
                }
                an.Fill(hj, "cj");
                this.dataGridView2.DataSource = hj.Tables["cj"];

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("没有设置打印机", "提示");
        }
        string san;
        private void button1_Click(object sender, EventArgs e)
        {
            san = Convert.ToString(this.dataGridView1.SelectedRows[0].Cells["name"].Value);
            this.label9.Text = san.ToString();
            this.panel2.Visible = true;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            chaxun();
        }
        DataSet aj = new DataSet("cj");
        public void chaxun()
        {
            try
            {
                SqlConnection con = new SqlConnection(str);
                string akn = @"select spID,spName,dcCzid,spJia from   dbo.commodity,[Ordering management],[Summary of consumption]
  where [Ordering management].dcID=[Summary of consumption].dcID 
and fangJai=100";
                SqlDataAdapter an = new SqlDataAdapter(akn, con);
                if (aj.Tables["cj"] != null)
                {
                    aj.Tables["cj"].Clear();
                }
                an.Fill(aj, "cj");
                this.dataGridView2.DataSource = hj.Tables["cj"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {this.panel1.Visible = true;
        }
        private void comboBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.panel1.Hide();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            pig = Convert.ToInt32(this.dataGridView2.SelectedRows[0].Cells[0].Value);
            der();
            panel1.Hide();
        }
        DataSet ds = new DataSet("as");
        private void button2_Click(object sender, EventArgs e)
        {
            //this.dataGridView1.Rows.Clear();
            DataTable dt = (DataTable)dataGridView1.DataSource;
            dt.Rows.Clear();
            dataGridView1.DataSource = dt;
            MessageBox.Show("删除成功", "提示");
        //    SqlConnection con = new SqlConnection(str);
        //    con.Open();
        //    try
        //    {
        //        string studer = dataGridView1.SelectedCells[0].Value.ToString();
        //        MessageBox.Show(studer);
        //        string ace = @"delete from commodity where spID='" + studer + "'";
        //        SqlCommand com = new SqlCommand(ace, con);
        //        int avg = com.ExecuteNonQuery();
        //        if (avg > 0)
        //        {
        //            MessageBox.Show("删除成功", "提示");
        //            der();
        //        }
        //        else
        //        {
        //            MessageBox.Show("删除失败", "提示");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        //    finally
        //    {
        //        con.Close();
        //    }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            this.panel2.Hide();
        }
        public void der()
        {
            SqlConnection con = new SqlConnection(str);
            ds = new DataSet("Studer");

            if (ds.Tables["Studer"]!=null)
            {
                ds.Tables["Studer"].Clear();
            }
            string an = @"SELECT  commodity.spID, commodity.spName, commodity.spJia, [Ordering management].dcCzid, [Summary of consumption].fangJai, 
                   [Summary of consumption].xfTime, [Summary of consumption].xfZong, [Ordering management].dcShul
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName CROSS JOIN
                   [Summary of consumption] where spID='" + pig + "' and fangJai=100";
            SqlDataAdapter snn = new SqlDataAdapter(an, con);
            snn.Fill(ds, "Studer");
            dataGridView1.DataSource = ds.Tables["Studer"];
        }

        private void button9_Click(object sender, EventArgs e)
        {
            xg();
            //der();
        }
        public void xg()
        {
            DataSet an = new DataSet("studer");
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string an1 = this.textBox2.Text.ToString();
            string pig = Convert.ToString(this.dataGridView1.SelectedCells[0].Value);
            string sql = @"update [Ordering management] set dcShul='"+an1+"' where dcID='"+pig+"'";
            SqlCommand com = new SqlCommand(sql, con);
            int i = Convert.ToInt32(com.ExecuteNonQuery());
            if (i > 0)
            {
                MessageBox.Show("修改成功", "提示");
                der();
            }
            else {
                MessageBox.Show("修改失败", "提示");
            }
        }

        private void label9_Click(object sender, EventArgs e)
        {

        }
    }
}
